import json

from config.config import OPENAPI_API_KEY_4, OPENAPI_API_BASE_4
from openai import OpenAI

client = OpenAI(
    api_key=OPENAPI_API_KEY_4,
    base_url=OPENAPI_API_BASE_4
)


def queryWeather(cityName: str):
    return f"{cityName}的天气很棒呦，出去玩正合适呢"


tools = [
    {
        "type": "function",
        "function": {
            "name": "queryWeather",
            "description": "根据传入的城市名称，查询指定城市的天气情况",
            "parameters": {
                "type": "object",
                "properties": {
                    "cityName": {
                        "type": "string",
                        "description": "城市名称：北京、天津、东京、杭州"
                    },
                },
                "required": ["cityName"]
            }
        }
    }
]
functionMaps = {
    "queryWeather": queryWeather
}


def getWeather(question: str):
    messages = [
        {
            "role": "user", "content": question
        }
    ]
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        tools=tools,
        tool_choice="auto"
    )
    for tool in response.choices[0].message.tool_calls:
        args = json.loads(tool.function.arguments)
        if functionMaps[tool.function.name] is not None:
            result = functionMaps[tool.function.name](**args)
        else:
            result = ""
        print(result)

    return response.choices[0].message.tool_calls
